package com.xmy.cultivate.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.xmy.cultivate.entity.Staff;
import com.xmy.cultivate.entity.StaffRankingDetail;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 * 排位详细 Mapper 接口
 * </p>
 *
 * @author hpiggy
 * @since 2023-05-16
 */
@Mapper
public interface StaffRankingDetailMapper extends BaseMapper<StaffRankingDetail> {

    /**
     * 查询排位数据列表
     *
     * @param wrapper
     * @param page
     * @return
     */
    @Select("SELECT staff_ranking_detail.* FROM staff_ranking_detail INNER JOIN role_admin ON staff_ranking_detail.staff_id=role_admin.admin_id ${ew.customSqlSegment}")
    IPage<StaffRankingDetail> listRankingData(@Param(Constants.WRAPPER) Wrapper wrapper, IPage<?> page);

    /**
     * 排位及工龄
     *
     * @param wrapper
     * @return
     */
    @Select("SELECT staff_ranking_detail.staff_id,staff_ranking_detail.staff_name,staff_ranking_id,ranking_num,role.`name` AS roleName,staff_ranking.`name`AS staffRankingName,staff_ranking.ranking_date FROM staff_ranking_detail LEFT JOIN role_admin ON role_admin.admin_id=staff_ranking_detail.staff_id LEFT JOIN role ON role.id=role_admin.role_id INNER JOIN staff_ranking ON staff_ranking.id=staff_ranking_detail.staff_ranking_id ${ew.customSqlSegment}")
    List<StaffRankingDetail> listStaffRankingDetail(@Param(Constants.WRAPPER) Wrapper wrapper);

    /**
     * 获取排位名次
     *
     * @param staffRankingId 排位编号
     * @param staffId 教师编号
     * @return
     */
    @Select("SELECT ranking_num FROM staff_ranking_detail WHERE staff_ranking_id=#{staffRankingId} AND staff_id=#{staffId} AND deleted = 0 LIMIT 1")
    Integer rankingNum(@Param("staffRankingId") Long staffRankingId, @Param("staffId") Long staffId);

    /**
     * 排位及工龄
     *
     * @param wrapper
     * @return
     */
    @Select("SELECT staff_id,staff_ranking_id,ranking_num,staff_ranking.`name` as staffRankingName, staff_ranking.ranking_date as rankingDate FROM staff_ranking_detail INNER JOIN staff_ranking ON staff_ranking_detail.staff_ranking_id = staff_ranking.id ${ew.customSqlSegment}")
    List<StaffRankingDetail> staffAnalysis(@Param(Constants.WRAPPER) Wrapper wrapper);
}
